import { defineStore } from 'pinia';
import * as salesApi from '@/api/sales';
import { Message } from '@arco-design/web-vue';
import { ref } from 'vue';

const useSalesStore = defineStore('sales', () => {
    async function getCompanies(param?: any) {
        const response: any = await salesApi.queryCompanyList(param)
        if (response.code === 20000) {
            return response.data.companies
        }
        return []
    }

    async function deleteCompany(param: { id?: number }) {
        const response: any = await salesApi.deleteCompany(param)
        if (response.code === 20000) {
            Message.success('移除成功');
        }
    }

    async function saveCompany(param: any) {
        if (param.id) {
            await salesApi.updateCompany(param);
        } else {
            await salesApi.createCompany(param);
        }
        Message.success('保存成功');
    }
    async function getSales(param?: any) {
        const response: any = await salesApi.querySalesList(param)
        if (response.code === 20000) {
            return response.data.salesmans
        }
        return []
    }

    async function deleteSales(param: { id?: number }) {
        const response: any = await salesApi.deleteSales(param)
        if (response.code === 20000) {
            Message.success('移除成功');
        }
    }

    async function saveSales(param: any) {
        if (param.id) {
            await salesApi.updateSales(param);
        } else {
            await salesApi.createSales(param);
        }
        Message.success('保存成功');
    }


    return { getCompanies, deleteCompany, saveCompany, getSales, deleteSales, saveSales }
});

export default useSalesStore;
